package com.hsh.dao.impl;

import com.hsh.bean.Blog;
import com.hsh.dao.OneBlogDao;
import com.hsh.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OneBlogDaoImpl extends DruidUtil implements OneBlogDao {

    @Override
    public Blog getOneBlogById(int bid) {
        Blog blog = new Blog();
        Connection conn = null;
        PreparedStatement state = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            String sql ="select * from blog where bid=?";
            state = conn.prepareStatement(sql);
            state.setInt(1,bid);
            rs = state.executeQuery();
            while(rs.next()) {
                blog.setBid(bid);
                blog.setNickname(rs.getString("nickname"));
                blog.setBlogText(rs.getString("blogtext"));
                blog.setFavor(rs.getInt("favor"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, state, rs);
        }
        return blog;
    }
}
